package com.example.eelly.aspectjprojectdemo;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created by 朱宇权 on 2016/4/8.
 */
@Aspect
public class AspectJClass {

    //......
    @Before("execution(* com.example.eelly.aspectjprojectdemo..*.onCreate(..)) && !within(com.example.eelly..*.*Base)&& !within(com.example.eelly..*.*Base*)&& !within(com.example.eelly..*.Base*)")
    public void AspectJLog(JoinPoint joinPoint){
        Log.e("ScienLogTag","onExecution :   "+joinPoint.toLongString());
        Log.e("ScienLogTag","onExecution :   "+joinPoint.getThis().getClass().getAnnotation(AspectJClass.PageIndex.class)!=null?joinPoint.getThis().getClass().getAnnotation(AspectJClass.PageIndex.class).label():"null");

    }

//    @Pointcut("execution(@AspectJClass.PageAction public * *..*.*(..)) && @annotation(ann)")
//    public void checkPermssion(AspectJClass.PageAction ann){
//    }

    @Before("execution(@AspectJClass.PageAction public * *..*.*(..)) && @annotation(ann)")
    public void checkPermssion2(AspectJClass.PageAction ann){
        Log.e("LOG","..... :   "+ann.actionName());
    }


//    @Before("checkPermssion(securityCheckAnnotation)")
//    public void check(JoinPoint joinPoint,AspectJClass.PageAction securityCheckAnnotation){
//        Log.e("LOG","..... :   "+securityCheckAnnotation.actionName());
//    }

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface PageIndex {
        /**
         * 页面名称
         */
        String label() default "11";
    }

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface PageAction {
        /**
         * 操作名
         */
        String actionName() default "name";
    }


    @Before(value = "execution(* *.onClick(android.view.View)) && args(view)",argNames = "view")
    public void getOnClick(View view){
        if(view instanceof Button)
            Log.i("ScienLogTag","ss:"+((Button)view).getText());
        else if(view instanceof TextView)
            Log.i("ScienLogTag","ss:"+((TextView)view).getText());
    }


    @Before("target(com.example.eelly.aspectjprojectdemo.jjj.page2)")
    public void pageIndexMethod(JoinPoint joinPoint) {
//        Method[] a=        joinPoint.getClass().getMethods();
        Log.i("ScienLogTag",",,,,>"+joinPoint.toString());
        if (joinPoint.getTarget() ==null){
            Log.i("ScienLogTag","aa");

        }else{
            Log.i("ScienLogTag","bb");
        }
    }


//    @Before("execution(* *.parse(com.google.gson.JsonElement))")
//    public void AspectJonResponse(JoinPoint joinPoint){
//        Log.e("ScienLogTag","onResponse :   "+joinPoint.toLongString());
//    }
    //......

    /*
    @Before("execution(* *.*(..))")
    public void AspectJLog(){
        Log.e("ScienLogTag","AspectJProject Watch Any Function Execution");
    }

    @Before("execution(* *.onCreate(..))")
    public void watchActivityCreate(JoinPoint joinPoint){
        Log.v("ScienLogTag", "watchActivityCreate Create Success.info:" + joinPoint.toString());
        Log.v("ScienLogTag", "watchActivityCreate Create Success.info:" + joinPoint.getArgs().length);
    }

    @Before("execution(* *.onClick(..))")
    public void whatOnClick(JoinPoint joinPoint){
        Log.v("ScienLogTag","Click"+joinPoint.getTarget().getClass().toString());
    }

    @Before("execution(* *.TestFunction(..))")
    public void watchTestFunction(JoinPoint joinPoint){
        Log.v("ScienLogTag", "watchTestFunction Success.info:" + joinPoint.getArgs()[0].toString());
        Log.v("ScienLogTag","Value: "+getParams(joinPoint.getArgs(),Integer.class));
    }


    public <T> Object getParams(Object[] args,T clazz){
        for (Object object:args) {
            if(object.getClass()== clazz){
                return object;
            }
        }
        return null;
    }
*/

    class UserRecord{
        public String getActivityName() {
            return ActivityName;
        }

        public void setActivityName(String activityName) {
            ActivityName = activityName;
        }

        public String getFragmentName() {
            return FragmentName;
        }

        public void setFragmentName(String fragmentName) {
            FragmentName = fragmentName;
        }

        /*
        public String getViewName() {
            return ViewName;
        }

        public void setViewName(String viewName) {
            ViewName = viewName;
        }
        */

        public Long getTime() {
            return Time;
        }

        public void setTime(Long time) {
            Time = time;
        }

        public int getUserID() {
            return UserID;
        }

        public void setUserID(int userID) {
            UserID = userID;
        }

        public int getTargetID() {
            return TargetID;
        }

        public void setTargetID(int targetID) {
            TargetID = targetID;
        }

        String ActivityName;
        String FragmentName;
//        String ViewName;
        Long Time;
        int UserID;
        int TargetID;
    }
}




